//
//  FormalOneHeaderView.m
//  Vest_native
//
//  Created by WangZhenyu on 2017/10/5.
//  Copyright © 2017年 WangZhenyu. All rights reserved.
//

#import "FormalOneHeaderView.h"
#import "AppDelegate.h"
#import "WebShowViewController.h"
#import "ApplyCreditViewController.h"
#import "LoadConfig.h"

@interface FormalOneHeaderView()<SDCycleScrollViewDelegate>{
    NSArray             *bannerArr;
    NSString                          *creditUrl;
}
@property (weak, nonatomic) IBOutlet UIView *oneView;
@property (weak, nonatomic) IBOutlet UIView *twoView;

@end
@implementation FormalOneHeaderView

- (id)initItem{
    self = [[[NSBundle mainBundle] loadNibNamed:@"FormalOneHeaderView" owner:self options:nil] lastObject];
    if (self) {
        
        self.bannerView.delegate = self;
        self.bannerView.pageControlStyle = SDCycleScrollViewPageContolStyleAnimated;
        self.bannerView.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        self.bannerView.autoScrollTimeInterval = 4.0;
        self.bannerView.hidesForSinglePage = YES;
        self.bannerView.bannerImageViewContentMode = UIViewContentModeScaleAspectFill;
        
        float bannerHeight = 150 * SCREEN_SCALE;
        [self.bannerView setFrame:CGRectMake(0, 0, SCREEN_WIDTH_DEVICE, bannerHeight)];
        
        
        [self setFrame:CGRectMake(0, 0, SCREEN_WIDTH_DEVICE, bannerHeight + 90)];

        NSDictionary *configDic = [[LoadConfig shareStance] getPerson];
        if (configDic.count > 0) {
            creditUrl = [configDic objectForKey:@"credit"];
        }
        [[LoadConfig shareStance] loadPersonSuccess:^(NSDictionary *dic) {
            creditUrl = [dic objectForKey:@"credit"];
        }];
        
    }
    return self;
}

- (void)setBanners:(NSArray *)arr{
    if (arr.count > 0) {
        bannerArr = arr;
        NSMutableArray *banners = [NSMutableArray array];
        for (int i = 0; i < arr.count; i++) {
            [banners addObject:[[arr objectAtIndex:i] objectForKey:@"imagePath"]];
        }
        self.bannerView.hidden = NO;
        self.bannerView.imageURLStringsGroup = banners;
        float bannerHeigthEx = 150 * SCREEN_SCALE;
        [self setFrame:CGRectMake(0, 0, SCREEN_WIDTH_DEVICE, 90 + bannerHeigthEx)];
    }else{
        self.bannerView.hidden = YES;
        [self setFrame:CGRectMake(0, 0, SCREEN_WIDTH_DEVICE, 90)];
    }
}

#pragma mark - SDCycleScrollViewDelegate

- (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index
{
    NSLog(@"点击了第%ld广告", index + 1);
    NSDictionary *contentDic = [bannerArr objectAtIndex:index];
    NSString *uri = [contentDic objectForKey:@"url"];
        
    if (uri.length > 0) {
        WebShowViewController *viewCon = [[WebShowViewController alloc] initWithNibName:@"WebShowViewController" bundle:nil];
        viewCon.urlStr = uri;
        AppDelegate *del = (AppDelegate*)[[UIApplication sharedApplication] delegate];
        [del.navigationController pushViewController:viewCon animated:YES];
    }
}

- (IBAction)daikuantuijianPress {
    AppDelegate *del = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    if (creditUrl.length > 0) {
        WebShowViewController *viewCon = [[WebShowViewController alloc] initWithNibName:@"WebShowViewController" bundle:nil];
        viewCon.urlStr = creditUrl;
        [del.navigationController pushViewController:viewCon animated:YES];
    }else{
        [[LoadConfig shareStance] loadPersonSuccess:^(NSDictionary *dic) {
            creditUrl = [dic objectForKey:@"credit"];
            WebShowViewController *viewCon = [[WebShowViewController alloc] initWithNibName:@"WebShowViewController" bundle:nil];
            viewCon.urlStr = creditUrl;
            [del.navigationController pushViewController:viewCon animated:YES];
        }];
    }
}

- (IBAction)xinyongkaPress {
    AppDelegate *del = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    ApplyCreditViewController *viewCon = [[ApplyCreditViewController alloc] initWithNibName:@"ApplyCreditViewController" bundle:nil];
    [del.navigationController pushViewController:viewCon animated:YES];
}


@end
